package com.hsc.eduservice.controller.front;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsc.commonutils.JwtUtils;
import com.hsc.commonutils.R;
import com.hsc.commonutils.ordervo.CourseWebVoOrder;
import com.hsc.eduservice.client.OrderClient;
import com.hsc.eduservice.entity.EduCourse;
import com.hsc.eduservice.entity.chapter.ChapterVo;
import com.hsc.eduservice.entity.frontvo.CourseFrontVo;
import com.hsc.eduservice.entity.frontvo.CourseWebVo;
import com.hsc.eduservice.service.EduChapterService;
import com.hsc.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.awt.image.ImageProducer;
import java.util.List;
import java.util.Map;

@RestController
//@CrossOrigin //gateway解决了可以注掉
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;


    /**
     * 分页查询带条件
     * @param page 当前页
     * @param limit 每页数量
     * @param courseFrontVo 查询条件
     * @return
     */
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable Long page,
                                @PathVariable Long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> eduCoursePage = new Page<>(page,limit);
        //返回所有分页数据
        Map<String,Object> map = courseService.getCourseFrontList(eduCoursePage,courseFrontVo);
        return R.ok().data(map);
    }

    /**
     * 根据课程id查询课程信息
     * @param courseId 课程id
     * @return
     */
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> chs = chapterService.getChapterVideoByCourseId(courseId);
        Boolean buyCourse = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chs).data("isBuy",buyCourse);
    }

    /**
     * 根据课程id查询课程信息，为其它服务远程调用提供数据
     * @param id 课程id
     * @return
     */
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }



}
